<?
/***********************************************************/
/* ColorButton, used to allows the user to choose colors
/* by Pablo Dall'Oglio 2004-2006
/***********************************************************/
class ColorButton
{
    /***********************************************************/
    /* Constructor Method
    /***********************************************************/
    function ColorButton($Description)
    {
        global $Pixmaps;
        $tooltip = &new GtkTooltips;
        $this->button = &new GtkButton('');
        $this->button->set_size_request(80, 32); // note 3
        $box = &new GtkHBox;
        $this->widget = new Box($box, $Description);//coloca a descricao de uso da cor
        $this->widget->modify_text(Gtk::STATE_NORMAL, GdkColor::parse('#ff0000'));
        $tooltip->set_tip($this->button, _a('Color Selection'), '');
        
        $pixmap = GtkImage::new_from_file($Pixmaps['cor']);
        $box->pack_start($this->button);//coloca o botao do lado esquerdo
        $box->pack_start($pixmap);//coloca a figura cor ao lado direito
                
        $this->connect_simple('clicked', array($this, 'SelColor'));
    }
    /***********************************************************/
    /* Changes the color
    /***********************************************************/
    function set_text($data)
    {   
        $this->button->modify_bg(Gtk::STATE_NORMAL,GdkColor::parse($data));
        
        $this->entry_data = $data;
        $label = $this->button->child;
        $label->modify_font(new PangoFontDescription("Arial Bold 14"));
        //$label->modify_text(Gtk::STATE_NORMAL, GdkColor::parse('#ffff00'));
        $this->button->modify_text(Gtk::STATE_NORMAL, GdkColor::parse('#ff0000'));

        $label->set_text($data);
    }

    /***********************************************************/
    /* Defines the callback
    /***********************************************************/
    function connect_simple($signal, $slot, $param = null)
    {
        $this->button->connect_simple($signal, $slot, $param);
    }

    /***********************************************************/
    /* Returns the color
    /***********************************************************/
    function get_text()
    {
        $label = $this->button->child;
        return $label->get();
    }

    /***********************************************************/
    /* Show the button
    /***********************************************************/
    function show()
    {
        $this->widget->show_all();
    }

    /***********************************************************/
    /* Define the size
    /***********************************************************/
    function set_usize($width, $height)
    {
        $this->button->set_size_request($width, $height);
    }

    /***********************************************************/
    /* Color Selection Dialog
    /***********************************************************/
    function SelColor()
    {
        $this->cswindow = $cswindow = new GtkWindow();
        $this->cswindow->connect_simple('key_press_event', array(&$this,'KeyTest'), $this->cswindow);
        $this->cswindow->set_position(GTK_WIN_POS_CENTER);
        $cswindow->show();
        $cswindow->set_title(_a('Color Selection'));
        
        $box = new GtkVBox();
        
        $this->cs = $cs = new GtkColorSelection();
        $rgb = $this->get_text();
        $int = rgb2int($rgb);
        //$this->cs->set_current_color($int[0], $int[1], $int[2]);
        $this->cs->connect('color-changed',array(&$this, 'KeyTest'),$cs); // note 2
           
        $box->pack_start($cs, false, false);
        
        $bt = new GtkButton('ok');
        $bt->connect_simple('clicked', array(&$this,'GetColor'));
        $bt->set_usize(-1,20);
        $box->pack_start($bt, false, false);
        
        $cswindow->add($box);
        $cswindow->show_all();
    }

    /***********************************************************/
    /* Returns the selected color
    /***********************************************************/
    function GetColor()
    {
        $ac = ($this->cs->get_color());
        $red = (int)($ac[0]*255);
        $green = (int)($ac[1]*255);
        $blue = (int)($ac[2]*255);
        
        $redc   = str_pad(dechex($red),2,'0', STR_PAD_LEFT);
        $greenc = str_pad(dechex($green),2,'0', STR_PAD_LEFT);
        $bluec  = str_pad(dechex($blue),2,'0', STR_PAD_LEFT);
        
        $html_color = "#".dechex($redc).dechex($green).dechex($blue);
        $this->set_text("#$redc$greenc$bluec");
        $this->cswindow->Hide();
    }

    /***********************************************************/
    /* KeyTest Method
    /***********************************************************/
    function KeyTest($obj, $window)
    {
        if ($obj->keyval == K_ESC)
        {
            $window->hide();
        }
    }
}
?>